package com.cdc.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

public class AnimationDrawableActivity extends AppCompatActivity {

    private ImageView mImage;
    private AnimationDrawable mAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_drawable);

        CheckBox mRepeat = (CheckBox)findViewById(R.id.checkBox);
        mRepeat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mAnimation.setOneShot(!isChecked);
            }
        });

        mImage = (ImageView)findViewById(R.id.ivKof);
        mImage.setBackgroundResource(R.drawable.anim_kof);
        mAnimation = (AnimationDrawable) mImage.getBackground();
    }

    public void startAnim(View view) {
        // 如果动画是oneShot模式，播放完之后，isRunning()仍然是true
        if(mAnimation.isRunning()){
            mAnimation.stop();
            mAnimation.start();
        } else {
            mAnimation.start();
        }
    }

    public void stopAnim(View view) {
        if(mAnimation.isRunning()){
            mAnimation.stop();
        }
    }
}
